// yb_mingpian/pages/ruzhu/tianjia.js
var t = getApp(),
  a = t.requirejs("core"),
  s = t.requirejs("wxParse/wxParse");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    background:"cloud://pinshang-ztqvr.7069-pinshang-ztqvr-1301803212/图片上传_wps图片.png",
    cate_list:[],
    obj: [],
    name:[],
    id:[],
    cate_id: 0,
    goods_name: '',
    price:'',
    img_id_array:0,
    stock:'',
    description:'',
    index:0,
    license:0,
    data:{},
    goods_id:0,
    pic_array:0,
    list:{},
    goods:0,
   
  },
// Inputcate_id(e){
//     this.setData({
//       cate_id: o.data.id[index]
//     })
//   console.log(this.data.cate_id);
// },
Inputgoods_name(e){
    this.setData({
      goods_name: e.detail.value
    })
  // console.log(this.data.tel);
},
Inputprice(e){
    this.setData({
      price: e.detail.value
    })
  // console.log(this.data.category);
},
Inputstock(e){
  this.setData({
    stock: e.detail.value
  })
// console.log(this.data.category);
},
Inputdescription(e){
  this.setData({
    description: e.detail.value
  })
// console.log(this.data.category);
},

chooseImageTap2: function (e) {
  var id1 = a.pdata(e).id1;
  this.upload(this, id1, 'chat_pic1');
},
upload: function (that, field, n, key = 0) {
  var _this = this;
  wx.showActionSheet({
    itemList: ['从相册中选择', '拍照'],
    itemColor: "#f7982a",
    success: function (res) {
      if (!res.cancel) {
        if (res.tapIndex == 0) {
          _this.chooseWxImage('album', that, field, n, key)
        } else if (res.tapIndex == 1) {
          _this.chooseWxImage('camera', that, field, n, key)
        }
      }
    }
  })
},
deleteImg: function (e) {
  var that = this;
  var imgs = that.data.imgs.undefined
  var index = e.currentTarget.dataset.index;//获取当前长按图片下标
  wx.showModal({
   title: '提示',
   content: '确定要删除此图片吗？',
   success: function (res) {
    if (res.confirm) {
     console.log('点击确定了');
     imgs.splice(index, 1);
    } else if (res.cancel) {
      console.log('点击取消了');
      return false;    
     }
    that.setData({
      imgs
    });
   }
  })
 },
 deleteImg1: function (e) {
  var that = this;
  var data = that.data.data.undefined
  var index = e.currentTarget.dataset.index;//获取当前长按图片下标
  wx.showModal({
   title: '提示',
   content: '确定要删除此图片吗？',
   success: function (res) {
    if (res.confirm) {
     console.log('点击确定了');
     data.splice(index, 1);
    } else if (res.cancel) {
      console.log('点击取消了');
      return false;    
     }
    that.setData({
      data
    });
   }
  })
 },
chooseWxImage: function (type_, that, field, n, key) {
  var _this = this,
    site = getApp().siteInfo,
    arr = that.data.data ? that.data.data : {},
    url = site.siteroot + '?i=' + site.uniacid + '&t=undefined&v=' + site.version + '&from=wxapp&c=entry&a=wxapp&do=index_uploadFile&path=' + n + '&m=yb_mingpian&sign=5201314';
    
  arr[field] = arr[field] ? arr[field] : [];

  wx.chooseImage({
    sizeType: ['original', 'compressed'],
    sourceType: [type_],
    success: function (res) {
      
      if (key == 0) {
        res.tempFilePaths.forEach(function (t) {
          wx.uploadFile({
            url: url,
            filePath: t,
            name: 'file_upload',
            success: function (res) {
              
              if (res.data != null && res.data != '') {

                arr[field] = arr[field].concat(res.data);
                // var data = [];
                that.setData({
                    data: arr
                  })
                  that.setData({
		                license: res.data,
                  })                  
                  console.log(that.data.data.undefined)
                //var data = a.json_parse(res.data);
                
                // if (data.code == 0) {
                //   arr[field] = arr[field].concat(data.info);
                //   that.setData({
                //     data: arr
                //   })
                // } else {
                //   a.error(data.msg)
                // }
              } else {
                  a.error(data.msg)
                }
            }
          })
        })
      } else {
        wx.uploadFile({
          url: url,
          filePath: res.tempFilePaths[0],
          name: 'file_upload',
          success: function (res) {
         
            if (res.data != null && res.data != '') {
              console.log(res.data)
              arr[field] = res.data;
              that.setData({
                  data: arr
                })
              // var data = a.json_parse(res.data);
              // if (data.code == 0) {
              //   //var arr = that.data.data;
              //   arr[field] = data.info;
              //   that.setData({
              //     data: arr
              //   })
              // } else {
              //   a.error(data.msg)
              // }
            } else {
                a.error('上传失败，请重试')
              }
          }
        })
      }
    }
  })
},
clickme: function () {
  var o = this
  console.log(o.data)
  console.log(o.data.goods_id)
   a.get("Card/GoodsDetail", {
    goods_id: o.data.goods_id,
   
    }, function (res) {
      console.log(res)
      o.setData({
        cate_id : res.info.cate_id
      })
      o.setData({
        description : res.info.description
      })
      o.setData({
        goods_name : res.info.goods_name
      })
      o.setData({
        pic_array : res.info.pic_array
      })
      o.setData({
        pic_array: o.data.pic_array.concat(o.data.data)
      });
      o.setData({
        price : res.info.price
      })
      o.setData({
        stock : res.info.stock
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata()
    var that=this
  console.log(options.id)
  that.setData({
    goods_id: options.id
  })
  if (that.data.goods_id>=1) {
    that.setData({
      goods: 1
    })
  }
  that.clickme()
  // console.log(that.data.goods_id)
  },
  
  bindPickerChange: function(e) {
    let o = this
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value

    })
    this.setData({
      cate_id: o.data.id[e.detail.value]

    })
    // console.log()
  },
  getdata: function(){
    var o = this
     a.get("Card/GoodsCate", {
        uid: getApp().getCache("userinfo").uid,
      }, function (res) {
        // console.log(res)
        o.setData({
          cate_list : res.cate_list
        })
        console.log(o.data.cate_list)
        let obj =[]
        let id =[]
      console.log(o.data.cate_list)
      for(let i = 0;i<o.data.cate_list.length; i++ ) {
        obj[i] = o.data.cate_list[i].cate_name
      }
      for(let i = 0;i<o.data.cate_list.length; i++ ) {
        id[i] = o.data.cate_list[i].cate_id
      }
      console.log(id)
      o.setData({
        obj :obj
      })
      o.setData({
        id :id
      })
      })
      
  },
	sub:function(){
    var o = this
    if ( o.data.cate_id == undefined) {
      wx.showToast({
        title: '请选择商品分类',
        icon: 'none',
      })
      return
    }
    if ( o.data.goods_name == undefined) {
      wx.showToast({
        title: '请填写商品名称',
        icon: 'none',
      })
      return
    }
    if ( o.data.img_id_array == 0) {
      wx.showToast({
        title: '请上传商品图片',
        icon: 'none',
      })
      return
    }
    if ( o.data.stock == undefined) {
      wx.showToast({
        title: '请输入商品价格',
        icon: 'none',
      })
      return
    }
    if ( o.data.price == undefined) {
      wx.showToast({
        title: '请输入商品库存',
        icon: 'none',
      })
      return
    }
    if ( o.data.description == undefined) {
      wx.showToast({
        title: '请添加商品详情',
        icon: 'none',
      })
      return
    }
    
    console.log(o.data.cate_id);
    console.log(o.data.goods_name);
    console.log(o.data.price);
  console.log(o.data.img_id_array);
  console.log(o.data.stock);
  console.log(o.data.description);
  
    a.get("Card/GoodsSave",{
      goods_id :0,
      uid: getApp().getCache("userinfo").uid,
      cate_id:o.data.cate_id,
      goods_name: o.data.goods_name,
      price: o.data.price,
      img_id_array: o.data.data,
      stock: o.data.stock,
      description: o.data.description,
    },function (res) {
      console.log(res)
      wx.showToast({
        title: res.info,
        icon: 'none',
      })
      // setTimeout(function () {
        // wx.redirectTo({
        //   url: '/packageA/pages/progress',
        // })
      // }.bind(o), 200)
      console.log(res)
    })
    wx.navigateTo({
      url: '/yb_mingpian/pages/ruzhu/guanli',
    })
  },
  sub1:function(){
    var o = this
    console.log(o.data.cate_id);
    console.log(o.data.goods_name);
    console.log(o.data.price);
  console.log(o.data.imgs);
  console.log(o.data.stock);
  console.log(o.data.description);
  
    a.get("Card/GoodsSave",{
      goods_id :o.data.goods_id,
      uid: getApp().getCache("userinfo").uid,
      cate_id:o.data.cate_id,
      goods_name: o.data.goods_name,
      price: o.data.price,
      img_id_array: o.data.pic_array,
      stock: o.data.stock,
      description: o.data.description,
    },function (res) {
     
      wx.navigateTo({
        url: '/yb_mingpian/pages/ruzhu/guanli',
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})